package com.wxit.algorithm.tree;

/**
 * 二叉树的三种遍历方式之递归
 */
public class TreeTraversal {

    public static void main(String[] args) {

       TreeNode root = new TreeNode( new TreeNode(new TreeNode(4), 2, null),
               1,
               new TreeNode(new TreeNode(5), 3, new TreeNode(6))
       );
       preOrder(root);
       System.out.println();
       inOrder(root);
       System.out.println();
       postOrder(root);
       System.out.println();
    }

    /**
     * <ur>前序遍历</ur>
     * @param node
     */
    static void preOrder(TreeNode node) {
        if (node == null) {
            return;
        }
        System.out.print(node.val + "\t");
        preOrder(node.left);
        preOrder(node.right);
    }

    /**
     * <ur>中序遍历</ur>
     * @param node
     */
    static void inOrder(TreeNode node) {
        if (node == null) {
            return;
        }
        inOrder(node.left); //左
        System.out.print(node.val + "\t"); //值
        inOrder(node.right);  //右
    }

    static void postOrder(TreeNode node) {
        if (node == null) {
            return;
        }
        postOrder(node.left); //左
        postOrder(node.right);  //右
        System.out.print(node.val + "\t"); //值
    }
}
